package cn.fanyamin.web.struts;

import java.sql.Timestamp;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.RoleService;
import cn.fanyamin.business.TaskService;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Goal;
import cn.fanyamin.domain.Role;
import cn.fanyamin.domain.User;

public class GoalAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(GoalAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		TaskService taskService = getWebApp().getTaskService();
		int goalType = NumberUtils.toInt(request.getParameter("goalType"));
	    User user = super.getUserFromSession(request);
	    
		List<Goal> goalList = null;
		if(goalType > 0)
			goalList = taskService.getGoalList(goalType, user.getUserID());
		else
			goalList = taskService.getAllGoalList(user.getUserID());
		
		request.setAttribute("goalList", goalList);
		logger.info("goalList: " + goalList);
		setCategories(request, UserCategory.CATEGORY_TYPE_TASK);
		
		setGoalTypes(request);
        return mapping.findForward("list");

	}
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		TaskService taskService = getWebApp().getTaskService();
		Goal goal = getGoalFromForm(form);
		if(goal == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		goal.setUserID(user.getUserID());
		
		int goalID = taskService.createGoal(goal);
		goal.setGoalID(goalID);
		logger.info("create goal: " + goal);
		return mapping.findForward("success");
	}
	
    private Goal getGoalFromForm(ActionForm form) {
		Goal goal = new Goal();
		LazyValidatorForm goalForm = (LazyValidatorForm) form;

		goal.setGoalID(NumberUtils.toInt((String) goalForm.get("goalID")));
		goal.setDescription((String) goalForm.get("description"));	
		goal.setGoalName((String) goalForm.get("goalName"));		
		goal.setGoalType(NumberUtils.toInt((String) goalForm.get("goalType")));		
		goal.setCategoryID(NumberUtils.toInt((String) goalForm.get("categoryID")));		
		
		String strDeadlline = (String) goalForm.get("deadline");
		if (StringUtils.isNotBlank(strDeadlline)) {
			goal.setDeadline(Timestamp.valueOf(strDeadlline));
		}
		return goal;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TaskService taskService = getWebApp().getTaskService();
        LazyValidatorForm goalFrom = (LazyValidatorForm)form;
        int goalID = NumberUtils.toInt((String)goalFrom.get("goalID"));
        Goal goal = taskService.retrieveGoal(goalID);
        request.setAttribute("goal", goal);
        setGoalTypes(request);    
        return mapping.findForward("edit");
    }
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        TaskService taskService = getWebApp().getTaskService();
        Goal goal = getGoalFromForm(form);
        taskService.updateGoal(goal);
        logger.info("update goal, goal=" + goal);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TaskService taskService = getWebApp().getTaskService();
        //LazyValidatorForm goalFrom = (LazyValidatorForm)form;
        int goalID = NumberUtils.toInt(request.getParameter("goalID"));
        taskService.deleteGoal(goalID);
        logger.info("delete goal, goalID=" + goalID);
        return mapping.findForward("success");
    }

    public ActionForward finish(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TaskService taskService = getWebApp().getTaskService();
        //LazyValidatorForm goalFrom = (LazyValidatorForm)form;
        int goalID = NumberUtils.toInt(request.getParameter("goalID"));
        Goal goal = taskService.retrieveGoal(goalID);
        goal.setEndTime(new Timestamp(System.currentTimeMillis()));
        taskService.updateGoal(goal);
        logger.info("finish goal, goalID=" + goalID);
        return mapping.findForward("success");
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        RoleService roleService = getWebApp().getRoleService();
	    List<Role> roleList = roleService.list();
	    request.setAttribute("roleList", roleList);
	    setCategories(request, UserCategory.CATEGORY_TYPE_TASK);
	    
	    setGoalTypes(request);
    	return mapping.findForward("add");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        TaskService taskService = getWebApp().getTaskService();
        LazyValidatorForm goalFrom = (LazyValidatorForm)form;
        int goalID = NumberUtils.toInt((String)goalFrom.get("goalID"));
        Goal goal = taskService.retrieveGoal(goalID);
        request.setAttribute("goal", goal);
        
        RoleService roleService = getWebApp().getRoleService();
	    List<Role> roleList = roleService.list();
	    request.setAttribute("roleList", roleList);
	    setCategories(request, UserCategory.CATEGORY_TYPE_TASK);
	    
	    setGoalTypes(request);
        return mapping.findForward("edit");
    }
}
